#include "video_renderer_manager.hpp"
#include "p2pms_util.hpp"
#include "p2pms_log.hpp"
#include "d3d_video_renderer.hpp"

namespace p2pms
{
//--------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------
VideoRendererManager::VideoRendererManager(IMainThreadExecutor* executor)
	: m_executor(executor)
{
	assert(m_executor);
}

//--------------------------------------------------------------------------------------------------
// 创建渲染器
//--------------------------------------------------------------------------------------------------
RenderId VideoRendererManager::CreateRenderer(const MediaInfo& media)
{
	RenderId render_id = GenerateUUID();
	
	m_renderers[render_id] = std::make_shared<D3DVideoRenderer>(media, m_executor);

	TrySetVideoTrack(media); // 尝试设置视频轨道

    LOG_INF("Created D3DVideoRenderer for media: {}, render_id: {}", media.media_id, render_id);

	return render_id;
}

//--------------------------------------------------------------------------------------------------
// 设置渲染窗口
//--------------------------------------------------------------------------------------------------
bool VideoRendererManager::SetRenderWindow(RenderId render_id, void* window_handle)
{
	LOG_INF("Setting render window for render_id: {}, window_handle: {}", render_id, window_handle);

	auto it = m_renderers.find(render_id);
	if (it != m_renderers.end()) {
		return it->second->SetRenderWindow(window_handle);
	}
	return false;
}

//--------------------------------------------------------------------------------------------------
// 刷新渲染
//--------------------------------------------------------------------------------------------------
void VideoRendererManager::RefreshRender(RenderId render_id)
{
	LOG_INF("Refreshing render for render_id: {}", render_id);

	auto it = m_renderers.find(render_id);
	if (it != m_renderers.end()) {
		it->second->OnPaint();
	}
}

//--------------------------------------------------------------------------------------------------
// 销毁渲染器
//--------------------------------------------------------------------------------------------------
void VideoRendererManager::DestroyRenderer(RenderId render_id)
{
	LOG_INF("Destroying renderer for render_id: {}", render_id);

	auto it = m_renderers.find(render_id);
	if (it != m_renderers.end()) {
		// 销毁渲染器之前先移除视频轨道
		it->second->SetVideoTrack(nullptr);
		m_renderers.erase(it);
	}
}

//--------------------------------------------------------------------------------------------------
// 渲染窗口大小变化事件
//--------------------------------------------------------------------------------------------------
void VideoRendererManager::OnRenderWindowSizeChanged(RenderId render_id)
{
	LOG_INF("Render window size changed for render_id: {}", render_id);

	auto it = m_renderers.find(render_id);
	if (it != m_renderers.end()) {
		it->second->OnWindowSizeChanged();
	}
}

//--------------------------------------------------------------------------------------------------
// 渲染窗口状态变化事件
//--------------------------------------------------------------------------------------------------
void VideoRendererManager::OnRenderWindowStateChanged(RenderId render_id, bool minimized)
{
	LOG_INF("Render window state changed for render_id: {}, minimized: {}", render_id, minimized);

	auto it = m_renderers.find(render_id);
	if (it != m_renderers.end()) {
		minimized ? it->second->Stop() : it->second->Start();
	}
}

//--------------------------------------------------------------------------------------------------
// 设置视频轨道
//--------------------------------------------------------------------------------------------------
void VideoRendererManager::TrySetVideoTrack(const MediaInfo& media)
{
	// 查找 track
	auto it = m_tracks.find(media);
	if (it != m_tracks.end()) {
		// 遍历渲染器并设置视频轨道
		for (const auto& pair : m_renderers) {
			if (pair.second->GetMediaInfo().media_id == media.media_id) {
				pair.second->SetVideoTrack(it->second);
				LOG_INF("Video track set for media: {}", media.media_id);
				break;
			}
		}
	}
}

//--------------------------------------------------------------------------------------------------
// 设置视频轨道
//--------------------------------------------------------------------------------------------------
void VideoRendererManager::AddVideoTrack(const MediaInfo& media, 
	rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track)
{
	LOG_INF("Adding video track for media: {}", media.media_id);

	if (media.media_id.empty() || !video_track) {
		LOG_ERR("Invalid media info or video track");
		return;
	}

	if (m_tracks.find(media) != m_tracks.end()) {
		LOG_ERR("Media already exists: {}", media.media_id);
		return;
	}
	m_tracks[media] = video_track;
	
	TrySetVideoTrack(media);
}

//--------------------------------------------------------------------------------------------------
// 
//--------------------------------------------------------------------------------------------------
void VideoRendererManager::RemoveVideoTrack(const MediaInfo& media)
{
	LOG_INF("Removing video track for media: {}", media.media_id);

	auto it = m_tracks.find(media);
	if (it != m_tracks.end()) {
		m_tracks.erase(it);
		LOG_INF("Video track removed for media: {}", media.media_id);
	} else {
		LOG_ERR("Video track not found for media: {}", media.media_id);
	}

	// 尝试更新渲染器的视频轨道
	for (const auto& pair : m_renderers) {
		if (pair.second->GetMediaInfo().media_id == media.media_id) {
			pair.second->SetVideoTrack(nullptr);
			LOG_INF("Video track cleared for media: {}", media.media_id);
		}
	}
}

} // namespace p2pms